﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NumberParser
{
    public class DecodeNumbers : IDecode
    {
        public DecodeNumbers()
        {

        }

        string[] IDecode.ReadFile(string filename)
        {
            var lines = new List<string>();
            try
            {
                using (StreamReader sr = new StreamReader(filename))
                {
                    string line;
                    // Read lines from the file until 
                    // the end of the file is reached. 
                    while ((line = sr.ReadLine()) != null)
                    {
                        lines.Add(line);
                    }
                }
            }
            catch (Exception e)
            {
                // Let the user know what went wrong.
                System.Windows.Forms.MessageBox.Show("The file could not be read: " + e.Message);
            }
            return lines.ToArray();
        }

        List<Digit> IDecode.Decode(string[] lines)
        {
            List<Digit> result = new List<Digit>();
            if (lines.Length % 4 != 0)
                throw new Exception("Inconsistent file");
            int fileDigitLines = lines.Length / 4;
            int digitNumber = 0;
            for (int j = 0; j < fileDigitLines; j++)
            {
                digitNumber = result.Count + j;
                var newLineDigit = result.Count + j;
                for (int i = j * 4; i < j * 4 + 4; i++)
                {
                    var newLine = lines[i];
                    if (i != j * 4)
                        digitNumber = newLineDigit - j;
                    //there is at least one digit
                    while (newLine.Length >= 5)
                    {
                        var part = newLine.Substring(0, 5);
                        var x = Line.TransformLine(part);
                        //check if line is valid (at least one visible square)
                        if (x.IsValid)
                        {
                            if (result.Count > digitNumber)
                            {
                                //the digit was already added
                                var dg = result[digitNumber];
                                dg.AddLine(x);
                            }
                            else
                            {
                                //add a new digit
                                var dg = new Digit(-1);
                                dg.AddLine(x);
                                result.Add(dg);
                            }
                        }
                        else
                            throw new Exception("invalid digit line");
                        digitNumber++;
                        //check the continue condition
                        newLine = newLine.Length > 8 ? newLine.Substring(8) :
                            newLine.Length > 5 ? newLine.Substring(6) : string.Empty;
                    }
                }
            }
            return result;
        }

        void IDecode.PrintDigit(Digit digit)
        {
            digit.ToString();
        }
    }
}
